package com.lijiajia.cloud.authserver.security;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lijiajia.cloud.common.security.user.ICloudUser;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;

/**
 * 用户详情 (基本信息)
 *
 * @author lijiajia
 * @since 2019/3/28
 */
@Data
@Accessors(chain = true)
public class BaseUser implements UserDetails, ICloudUser {
	/**
	 * 用户id
	 */
	protected String userId;
	/**
	 * 登录名
	 */
	protected String loginName;
	/**
	 * 登陆方式
	 */
	protected String loginType;
	/**
	 * 密码
	 */
	@JsonIgnore
	protected String password;
	/**
	 * 邮箱
	 */
	protected String email;
	/**
	 * 电话
	 */
	protected String telephone;
	/**
	 * 头像
	 */
	protected String avatar;
	/**
	 * 真实姓名
	 */
	protected String realName;
	/**
	 * 启用
	 */
	protected Boolean enabled;
	/**
	 * 删除
	 */
	protected Boolean deleted;
	/**
	 * 加入时间
	 */
	protected LocalDateTime joinTime;
	/**
	 * 权限
	 * 用户角色 ROLE_ 开头
	 * 例如 : ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_USER_ADMIN, ROLE_DICT_ADMIN
	 * 接口权限 任意
	 * 例如 : SYS_DICT_ADD, SYS_DICT_UPDATE, SYS_DICT_MODIFY
	 */
	protected Collection<? extends GrantedAuthority> authorities;

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	@JsonIgnore
	public String getUsername() {
		return userId;
	}

	@Override
	public boolean isAccountNonExpired() {
		// 账号永不过期
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// 账号永不锁定
		return !deleted;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// 用户的凭据（密码）是否已过期
		return true;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}
}
